﻿/**************************************************************************************************************
 * Class name:  T_amCharts.cs
 * Description: amCharts 测试
 *              http://www.amcharts.com/
 * Author:      Ju2ender
 * Create:      2012-12-04 Tuesday 14:40:45
 * Update:      2012-12-04 Tuesday 14:40:45
 **************************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DearLee.Toolkit.IO;
using DearLee.Toolkit.Web;
using LitJson;

namespace Web {

  public partial class T_amCharts : BasePage {

    protected void Page_Load(object sender, EventArgs e) {

      if (!IsPostBack) {

        List<ListItem> list = null;
        
        list = new List<ListItem> { 
          new ListItem {
            Text = "武將牌",
            Value = "260"
          },
          new ListItem {
            Text = "基本牌",
            Value = "201"
          },
          new ListItem {
            Text = "锦囊牌",
            Value = "65"
          },
          new ListItem {
            Text = "装备牌",
            Value = "39"
          },
          new ListItem {
            Text = "收藏牌",
            Value = "19"
          }
        };
      }
    }

    public void BuildChartData(List<ListItem> list) {

      string json, path;

      json = ToJson(list);
      path = "~/json/chartData.json";
      path = Server.MapPath(path);

      StreamHelper.WriteData(path, false, System.Text.Encoding.UTF8, json);
    }

    /// <summary>
    /// 将 List<ListItem> 转换为 JSON 字符串
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public string ToJson(List<ListItem> list) {

      string json;

      json = "{\"chartData\":[";

      foreach (ListItem item in list) {
        json += "{\"text\":\"" + item.Text + "\",\"value\":\"" + item.Value + "\"},";
      }

      json = json.TrimEnd(',');
      json += "]}";

      return json;
    }
  }
}